<template>
  <view></view>
</template>

<script>
import { bindWechat, getWechatCode } from '@/api/login/wechat.js'
import { wechatPayment, polymerizationPay } from '@/api/payment/index.js'


export default {
  data () {
    return {
      sn: '',
      serviceType: '',
    }
  },
  onLoad (e) {
    this.sn = e.sn
    this.serviceType = e.serviceType || ''

    bindWechat().then(bindRes => {
      getWechatCode().then(code => {
        this.codes = code
        if (this.serviceType) {
          polymerizationPay('DINPAY', 'MP', { sn: this.sn, serviceType: this.serviceType, orderType: 'ORDER', code }).then(res => {
            if (res.code === 200) {
              const prepay = JSON.parse(res.data)
              uni.requestPayment({
                provider: 'wxpay', // 服务提提供商
                timeStamp: prepay.timeStamp, // 时间戳
                nonceStr: prepay.nonceStr, // 随机字符串
                package: prepay.package,
                signType: prepay.signType, // 签名算法
                paySign: prepay.paySign, // 签名
                // success: function (res) { uni.navigateTo({ url: '/subpackage/order/orderInfo?sn=' + that.sn }) },
                success: function (res) {
                  uni.showToast({ icon: 'none', title: '支付成功', mask: true })
                  getPaymentOnBehalf(this.sn).then(res => {
                    if (res.code === 200) {
                      // uni.navigateTo({ url: "/pages/cashier/share?params=" + encodeURIComponent(res.data.url), })
                      uni.navigateBack()
                    }
                  })
                },
                fail: function (err) { console.log('支付失败', err) }
              })
            }
          })

        } else {

          wechatPayment('ORDER', this.sn, code).then(response => {
            if (response.code === 200) {
              const weChatPayData = response.data
              // 调起微信支付
              let that = this
              uni.requestPayment({
                provider: 'wxpay', // 服务提提供商
                timeStamp: weChatPayData.timeStamp, // 时间戳
                nonceStr: weChatPayData.nonceStr, // 随机字符串
                package: weChatPayData.package,
                signType: weChatPayData.signType, // 签名算法
                paySign: weChatPayData.paySign, // 签名
                code: that.codes,
                success: function (res) {
                  uni.showToast({ icon: 'none', title: '支付成功', mask: true })
                  getPaymentOnBehalf(this.sn).then(res => {
                    if (res.code === 200) {
                      // uni.navigateTo({ url: "/pages/cashier/share?params=" + encodeURIComponent(res.data.url), })
                      uni.navigateBack()
                    }
                  })
                },
                fail: function (err) { uni.showToast({ icon: 'none', title: '支付失败', mask: true }) }
              })
            }
          })

        }

      })
    })
  },
}
</script>

<style lang="scss" scoped>
</style>
